{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from solver import Rectangle, Container, Instance\n",
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "instance = Instance(\n",
    "    container=Container(width=40, height=15),\n",
    "    rectangles=[\n",
    "        Rectangle(width=11, height=3),\n",
    "        Rectangle(width=13, height=3),\n",
    "        Rectangle(width=9, height=2),\n",
    "        Rectangle(width=7, height=2),\n",
    "        Rectangle(width=9, height=3),\n",
    "        Rectangle(width=7, height=3),\n",
    "        Rectangle(width=11, height=2),\n",
    "        Rectangle(width=13, height=2),\n",
    "        Rectangle(width=11, height=4),\n",
    "        Rectangle(width=13, height=4),\n",
    "        Rectangle(width=3, height=5),\n",
    "        Rectangle(width=11, height=2),\n",
    "        Rectangle(width=2, height=2),\n",
    "        Rectangle(width=11, height=3),\n",
    "        Rectangle(width=2, height=3),\n",
    "        Rectangle(width=5, height=4),\n",
    "        Rectangle(width=6, height=4),\n",
    "        Rectangle(width=12, height=2),\n",
    "        Rectangle(width=1, height=2),\n",
    "        Rectangle(width=3, height=5),\n",
    "        Rectangle(width=13, height=5),\n",
    "        Rectangle(width=12, height=4),\n",
    "        Rectangle(width=1, height=4),\n",
    "        Rectangle(width=5, height=2),\n",
    "        Rectangle(width=6, height=2),  # add to make tight\n",
    "        Rectangle(width=6, height=3),  # add to make infeasible\n",
    "    ],\n",
    ")\n",
    "\n",
    "with open(Path(\"./instances/instance_1.json\"), \"w\") as file:\n",
    "    file.write(instance.model_dump_json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "for i in range(10):\n",
    "    container = Container(width=100, height=50)\n",
    "    rectangles = []\n",
    "    for j in range(50):\n",
    "        rectangles.append(\n",
    "            Rectangle(width=random.randint(3, 20), height=random.randint(3, 20))\n",
    "        )\n",
    "    instance = Instance(container=container, rectangles=rectangles)\n",
    "    with open(Path(f\"./instances/instance_random_{i}.json\"), \"w\") as file:\n",
    "        file.write(instance.model_dump_json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(Path(\"./instances/instance_1.json\"), \"r\") as file:\n",
    "    print(Instance.model_validate_json(file.read()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "instances = {}\n",
    "\n",
    "with open(Path(\"./Hopper_c.txt\"), \"r\") as file:\n",
    "    rectangles = []\n",
    "    container = None\n",
    "    instance_name = None\n",
    "    num_rectangles = None\n",
    "    for line in file.readlines():\n",
    "        line = line.strip()\n",
    "        line = line.replace(\"\\t\", \" \")\n",
    "        if not instance_name:\n",
    "            instance_name = line.strip()\n",
    "            continue\n",
    "        elif not container:\n",
    "            container = Container(\n",
    "                width=int(line.split()[0]), height=int(line.split()[1])\n",
    "            )\n",
    "            continue\n",
    "        elif not num_rectangles:\n",
    "            num_rectangles = int(line.strip())\n",
    "            continue\n",
    "        # empty line indicates end of instance\n",
    "        elif line.strip() == \"\":\n",
    "            instances[instance_name] = Instance(\n",
    "                container=container, rectangles=rectangles\n",
    "            )\n",
    "            assert len(rectangles) == num_rectangles\n",
    "            instance_name = None\n",
    "            rectangles = []\n",
    "            container = None\n",
    "            num_rectangles = None\n",
    "            continue\n",
    "        else:\n",
    "            rectangles.append(\n",
    "                Rectangle(width=int(line.split()[0]), height=int(line.split()[1]))\n",
    "            )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "for instance_name, instance_data in instances.items():\n",
    "    with open(Path(f\"./instances/hopper/{instance_name}.json\"), \"w\") as file:\n",
    "        file.write(instance_data.model_dump_json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['hopper/C1_1.json',\n",
       " 'hopper/N7d.json',\n",
       " 'hopper/README.md',\n",
       " 'hopper/N7a.json',\n",
       " 'hopper/TC6_3.json',\n",
       " 'hopper/N3e.json',\n",
       " 'hopper/TC6_1.json',\n",
       " 'hopper/TN4b.json',\n",
       " 'hopper/C1_3.json',\n",
       " 'hopper/N3d.json',\n",
       " 'hopper/C3_1.json',\n",
       " 'hopper/C7_2.json',\n",
       " 'hopper/N1a.json',\n",
       " 'hopper/TN6d.json',\n",
       " 'hopper/TN5e.json',\n",
       " 'hopper/N4e.json',\n",
       " 'hopper/TN5a.json',\n",
       " 'hopper/TN5d.json',\n",
       " 'hopper/TN5b.json',\n",
       " 'hopper/N1b.json',\n",
       " 'hopper/N2c.json',\n",
       " 'hopper/TN2b.json',\n",
       " 'hopper/N6c.json',\n",
       " 'hopper/TC1_3.json',\n",
       " 'hopper/N1e.json',\n",
       " 'hopper/TN1c.json',\n",
       " 'hopper/N4d.json',\n",
       " 'hopper/N7b.json',\n",
       " 'hopper/N4a.json',\n",
       " 'hopper/C2_1.json',\n",
       " 'hopper/C3_3.json',\n",
       " 'hopper/C6_1.json',\n",
       " 'hopper/TC4_1.json',\n",
       " 'hopper/TN3c.json',\n",
       " 'hopper/TN1e.json',\n",
       " 'hopper/N3b.json',\n",
       " 'hopper/N3a.json',\n",
       " 'hopper/TC3_1.json',\n",
       " 'hopper/N4b.json',\n",
       " 'hopper/TN4a.json',\n",
       " 'hopper/N7c.json',\n",
       " 'hopper/TN6b.json',\n",
       " 'hopper/N7e.json',\n",
       " 'hopper/TN5c.json',\n",
       " 'hopper/TC7_2.json',\n",
       " 'hopper/N6b.json',\n",
       " 'hopper/N5a.json',\n",
       " 'hopper/C4_1.json',\n",
       " 'hopper/TN1a.json',\n",
       " 'hopper/C3_2.json',\n",
       " 'hopper/TC2_3.json',\n",
       " 'hopper/TN2c.json',\n",
       " 'hopper/TC7_3.json',\n",
       " 'hopper/TC4_3.json',\n",
       " 'hopper/TN7c.json',\n",
       " 'hopper/N2d.json',\n",
       " 'hopper/TC5_3.json',\n",
       " 'hopper/TN3d.json',\n",
       " 'hopper/TC5_2.json',\n",
       " 'hopper/TC6_2.json',\n",
       " 'hopper/TC4_2.json',\n",
       " 'hopper/TC1_1.json',\n",
       " 'hopper/TN7d.json',\n",
       " 'hopper/TC3_2.json',\n",
       " 'hopper/C6_3.json',\n",
       " 'hopper/TN1b.json',\n",
       " 'hopper/TN7e.json',\n",
       " 'hopper/C4_2.json',\n",
       " 'hopper/N6e.json',\n",
       " 'hopper/C5_3.json',\n",
       " 'hopper/N5c.json',\n",
       " 'hopper/TN4e.json',\n",
       " 'hopper/TC7_1.json',\n",
       " 'hopper/TN2d.json',\n",
       " 'hopper/C5_1.json',\n",
       " 'hopper/C1_2.json',\n",
       " 'hopper/TC5_1.json',\n",
       " 'hopper/N5b.json',\n",
       " 'hopper/TN3e.json',\n",
       " 'hopper/C7_1.json',\n",
       " 'hopper/TC1_2.json',\n",
       " 'hopper/N3c.json',\n",
       " 'hopper/TN3b.json',\n",
       " 'hopper/C4_3.json',\n",
       " 'hopper/N1c.json',\n",
       " 'hopper/TN6a.json',\n",
       " 'hopper/C6_2.json',\n",
       " 'hopper/TN1d.json',\n",
       " 'hopper/N2a.json',\n",
       " 'hopper/TN4d.json',\n",
       " 'hopper/TC2_2.json',\n",
       " 'hopper/C5_2.json',\n",
       " 'hopper/TN7a.json',\n",
       " 'hopper/N4c.json',\n",
       " 'hopper/TC2_1.json',\n",
       " 'hopper/TC3_3.json',\n",
       " 'hopper/TN7b.json',\n",
       " 'hopper/TN2a.json',\n",
       " 'hopper/C2_3.json',\n",
       " 'hopper/N2e.json',\n",
       " 'hopper/TN2e.json',\n",
       " 'hopper/N1d.json',\n",
       " 'hopper/N5e.json',\n",
       " 'hopper/TN6c.json',\n",
       " 'hopper/TN3a.json',\n",
       " 'hopper/N2b.json',\n",
       " 'hopper/N6a.json',\n",
       " 'hopper/N5d.json',\n",
       " 'hopper/C2_2.json',\n",
       " 'hopper/C7_3.json',\n",
       " 'hopper/TN6e.json',\n",
       " 'hopper/TN4c.json',\n",
       " 'hopper/N6d.json']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "instance_names = [\n",
    "    str(instance_name.relative_to(\"./instances\"))\n",
    "    for instance_name in Path(\"./instances/hopper\").iterdir()\n",
    "    if instance_name.is_file()\n",
    "]\n",
    "instance_names"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mo312",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
